package com.fengye.school.model.convertors;

import com.fengye.school.model.school.enums.SexType;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

/**
 * 泛型指的是你在实体用的类型，第二个是指需要映射到数据库的类型对应的java中的类型
 */
@Converter(autoApply = true)
public class SexTypeConverter implements AttributeConverter<SexType, String> {

    @Override
    public String convertToDatabaseColumn(SexType attribute) {
        return attribute.getName();
    }

    @Override
    public SexType convertToEntityAttribute(String dbData) {
        for (SexType sexType : SexType.values()) {
            if (dbData.equals(sexType.getName())) {
                return sexType;
            }
        }
        return null;
    }
}